package com.storyroad.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.storyroad.responseClasses.StoryView;
//import com.storyroad.responseClasses.StoryView;
import com.storyroad.utils.DBConnection;
import com.storyroad.utils.SearchEngine;

/**
 * Servlet implementation class Search
 */
public class Search extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Search() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * Carries out the search operation on the story fragments based on the data provided by the user. A list of
	 * {@link com.storyroad.responseClasses.StoryView} instances which contain the search results is returned to the
	 * front-end.Please refer to {@link com.storyroad.utils.SearchEngine} for a detailed explanation on how the search
	 * engine works.
	 * @param request HttpServletRequest object that contains data coming from front-end
	 * @param response HttpServletResponse object that contains data which is prepared after processing the incoming data
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ArrayList<StoryView> searchResults = new ArrayList<StoryView>();
		PrintWriter out = response.getWriter();
		Connection conn = new DBConnection().connect();
		String f_requestType = request.getParameter("request_type");
		String f_keywords = request.getParameter("keywords");
		String f_date = request.getParameter("date");
		String f_location = request.getParameter("location");
		if (f_requestType == null || !f_requestType.equals("Android")) {
			f_requestType = "Web";
		}

		if (conn == null) {
			out.println("Connection error");
		} else {
			SearchEngine searchEngine = new SearchEngine(conn, f_keywords, f_date, f_location);
			// If location information is provided, search for story fragments based on this information.
			if (f_location != null && !f_location.equals("")) {
				searchEngine.locationSearch();
			}
			// If date is provided, search for story fragments based on the this information.
			if (f_date != null && !f_date.equals("")) {
				searchEngine.dateSearch();
			}
			if (f_keywords != null && !f_keywords.equals("")) {
				searchEngine.keywordSearch();
			}
			searchResults = searchEngine.generateResults();

			JSONObject searchResultObject = new JSONObject();
			try {
				searchResultObject.put("searchResults", searchResults);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print(searchResultObject.toString());
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
